package com.demo.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.dao.KeShiBiaoMapper;
import com.demo.dao.YiShengMapper;
import com.demo.dao.YiShengPaiBanMapper;
import com.demo.entity.KeShiBiao;
import com.demo.entity.YiSheng;
import com.demo.entity.YiShengPaiBan;

@Service
public class PaibanService{
	@Autowired
	private YiShengMapper yiShengMapper;
	
	@Autowired
	private YiShengPaiBanMapper yiShengPaiBanMapper;
	
	@Autowired
	private KeShiBiaoMapper keShiBiaoMapper;
       
	public void panbanZIdong() {
		Date date = new Date();
		SimpleDateFormat s = new SimpleDateFormat("MM/dd/yyyy");
		List<String> dateList = new ArrayList<String>();
		for(int i=0;i<=7;i++) {
			long time=(date.getTime()/1000)+60*60*24*i;
			Date newDate=new Date();
			newDate.setTime(time*1000);
			dateList.add(s.format(newDate));
		}
		List<KeShiBiao>  keShiBiaos = keShiBiaoMapper.selectAll();
		if(keShiBiaos.size()>0) {
			for(int i=0;i<keShiBiaos.size();i++) {
				List<YiSheng> lisyiList = yiShengMapper.findyishengBykeshi(String.valueOf(keShiBiaos.get(i).getId()));
				if(lisyiList.size()>0) {
					for (YiSheng yiSheng : lisyiList) {
						for (String datec : dateList) {
							YiShengPaiBan yiShengPaiBan = new YiShengPaiBan();
							int id=0;
							if(yiShengPaiBanMapper.findMaxId()==null) {
								id = 1;
							}else {
								id = Integer.valueOf(yiShengPaiBanMapper.findMaxId())+1;
							}
							yiShengPaiBan.setId(id);
							yiShengPaiBan.setKeshi(String.valueOf(keShiBiaos.get(i).getId()));
							yiShengPaiBan.setYisheng(String.valueOf(yiSheng.getId()));
							yiShengPaiBan.setShijianbianhao("T");
							yiShengPaiBan.setJiuzhendidian(keShiBiaos.get(i).getKeshi()+"门诊部");
							yiShengPaiBan.setJiuzhengshijian(datec);
							yiShengPaiBan.setFeiyong("12");
							System.out.println(yiShengPaiBan.toString());
							yiShengPaiBanMapper.insert(yiShengPaiBan);
						}
					}
				}
			}
		}
		
	}
	
	public boolean checkdateuable() throws ParseException {
		String datemax = yiShengPaiBanMapper.findMaxDate();
		Date datenow = new Date();
		Calendar calendar  =   Calendar.getInstance();
		calendar.setTime(datenow); 
		calendar.add(calendar.DATE,7);
		datenow=calendar.getTime();
		SimpleDateFormat s = new SimpleDateFormat("MM/dd/yyyy");
		datemax = (datemax==null||datemax=="null"||datemax==""||("").equals(datemax))?s.format(datenow):datemax;
		System.out.println(datemax);
		Date doDate = s.parse(datemax);
		if(datenow.compareTo(doDate)==-1) {
			return false;
		}else {
			return true;
		}
	}
	
	public List<YiShengPaiBan>  findavaliablePanban(String keshi){
		Date datenow = new Date();
		SimpleDateFormat s = new SimpleDateFormat("MM/dd/yyyy");
		return yiShengPaiBanMapper.findavaliablePanban(s.format(datenow),keshi);
		
	}
	
	public List<String> findADateList(String keshi){
		Date datenow = new Date();
		SimpleDateFormat s = new SimpleDateFormat("MM/dd/yyyy");
		return yiShengPaiBanMapper.findADateList(s.format(datenow), keshi);
	}
	
	public List<String> findADateListByyIsheng(String keshi,String yisheng){
		Date datenow = new Date();
		SimpleDateFormat s = new SimpleDateFormat("MM/dd/yyyy");
		return yiShengPaiBanMapper.findADateListByyIsheng(keshi,yisheng,s.format(datenow));
	}
	
	    
	public List<YiSheng> findAYishengBuKeshi(String keshi){
		List<String> list = yiShengPaiBanMapper.findAYishengBuKeshi(keshi);
		List<YiSheng> yiShengs = new ArrayList<YiSheng>();
		if(list.size()>0) {
			for (String string : list) {
				if(string!=null&&string!="") {
					yiShengs.add(yiShengMapper.selectByPrimaryKey(Integer.valueOf(string)));
				}
			}
		}
		return yiShengs;	
	}

	public List<YiSheng> chooseDoctorByDateKeAKeshi(String keshi, String date) {
		List<String> list = yiShengPaiBanMapper.findAYishengBuKeshiDate(keshi, date);
		List<YiSheng> yiShengs = new ArrayList<YiSheng>();
		if(list.size()>0) {
			for (String string : list) {
				if(string!=null&&string!="") {
					yiShengs.add(yiShengMapper.selectByPrimaryKey(Integer.valueOf(string)));
				}
			}
		}
		return yiShengs;
	}
}
